package com.fxkj.cloud.drms.ui.listener;

import com.fxkj.cloud.dbs.common.constants.WsMsgEnum;
import com.fxkj.cloud.dbs.common.event.stream.RemoteNoticeEvent;
import com.fxkj.cloud.dbs.common.model.ws.FxWsMsg;
import com.fxkj.cloud.dbs.common.model.ws.NoticeInfo;
import com.fxkj.cloud.dbs.common.model.ws.SendType;
import com.fxkj.cloud.drms.domain.ws.service.FxWsClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;

/**
 * <p>description: 远程用户通知事件监听器</p>
 * @author ww
 * @date 2025/4/22 15:21
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class RemoteNoticeEventListener {

    private final FxWsClient wsClient;

    @Bean
    public Consumer<RemoteNoticeEvent> notice() {
        return event -> {
            log.trace("收到远程用户通知事件:{}", event);
            Set<UUID> userIds = event.getUserId();
            NoticeInfo noticeInfo = event.getNoticeInfo();
            SendType sendType = event.getSendType();
            WsMsgEnum msgType;
            if (sendType.equals(SendType.HOMEPAGE_NOTIFICATION)) {
                msgType = WsMsgEnum.HOME_ANNOUNCEMENT;
            } else if (sendType.equals(SendType.ON_SITE_NOTIFICATION)) {
                msgType = WsMsgEnum.NOTICE;
            } else {
                return;
            }
            wsClient.pushNotificationToUsers(userIds, new FxWsMsg(msgType, noticeInfo));
        };
    }
}
